package com.xvyy.tingshu.search.api;

import com.xvyy.tingshu.common.constant.RedisConstant;
import com.xvyy.tingshu.common.result.Result;
import com.xvyy.tingshu.search.service.ItemService;
import com.xvyy.tingshu.vo.search.AlbumInfoIndexVo;
import com.xvyy.tingshu.annotation.Cacheable;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Tag(name = "专辑详情管理")
@RestController
@RequestMapping("api/search/albumInfo")
@SuppressWarnings({"unchecked", "rawtypes"})
public class ItemApiController {

    @Autowired
    private ItemService itemService;


    @PostMapping("/onAlbumEs/{albumId}")
    @Operation(summary = "上架专辑到ES")
    public Result onAlbumEs(@PathVariable(value = "albumId") Long albumId) {
        itemService.onAlbumEs(albumId);
        return Result.ok();
    }

    @PostMapping("/offAlbumEs/{albumId}")
    @Operation(summary = "下架专辑到ES")
    public Result offAlbumEs(@PathVariable(value = "albumId") Long albumId) {
        itemService.offAlbumEs(albumId);
        return Result.ok();
    }


    @PostMapping("/onAlbumEsAll")
    @Operation(summary = "从MySQL中查询专辑数，批量上架到ES")
    public Result onAlbumEsAll() {
        //TODO
        itemService.onAlbumEsAll();
        return Result.ok();
    }

    @PostMapping("/onAlbumEsAllTest")
    @Operation(summary = "从MySQL中查询专辑数，批量上架到ES")
    public Result onAlbumEsAllTest() {
        for (int i = 1; i <= 1577; i++) {
            itemService.onAlbumEs((long) i);
        }
        return Result.ok();
    }

    // http://192.168.217.1:8500/api/search/albumInfo/936
    // http://192.168.217.1:8500/api/search/albumInfo/1499
    @GetMapping("/{albumId}")
    @Operation(summary = "根据专辑id查询专辑详情")
//    @TsLogin
    @Cacheable(//#{#args[0]} 代表方法参数列表的第1个参数
            cacheKey = RedisConstant.CACHE_INFO_PREFIX + "#{#args[0]}", //缓存的key
            distributedLockKey = RedisConstant.CACHE_LOCK_SUFFIX + "#{#args[0]}", // 分布式锁的key
            distributedBloomKey = "#{#args[0]}",// 分布式布隆过滤器的key
            distributedLockSwitch = true, // 分布式锁开关 默认开启
            distributedBloomSwitch = true) // 分布式布隆过滤器开关 默认开启
    public Result getAlbumInfoByAlbumId(@PathVariable(value = "albumId") Long albumId) {
        Map<String, Object> map = itemService.getAlbumInfoByAlbumId(albumId);
        return Result.ok(map);
    }

    @Operation(summary = "排行榜提前预热")
    @GetMapping("/preHitRank")
    public Result preHitRank() {
        itemService.preHitRank();
        return Result.ok();
    }


    // http://192.168.217.1:8500/api/search/albumInfo/findRankingList/1/hotScore
    @GetMapping("/findRankingList/{c1Id}/{dimension}")
    @Operation(summary = "查询排行榜数据")
    public Result findRankingList(
            @PathVariable("c1Id") Integer c1Id,
            @PathVariable("dimension") String dimension) {
        List<AlbumInfoIndexVo> albumInfoIndexVoList = itemService.findRankingList(c1Id, dimension);
        return Result.ok(albumInfoIndexVoList);
    }

    @GetMapping("/cacheableSpELTest")
    @Operation(summary = "测试SpringEL表达式")
    public Result cacheableSpELTest() {
        boolean aBoolean = itemService.cacheableSpELTest();
        return Result.ok(aBoolean);
    }
}

